#pragma once
#define STACK_INIT_SIZE 20
#define STACKINCRENT 10
typedef char ElemType;
typedef struct {
    ElemType *base;// 头
    ElemType *top;// 尾
    int stackSize;
}Stack;

// 创建
void InitStack(Stack *s) {
    s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if (!s->base)
        exit(0);
    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}

void Push(Stack *s, ElemType e) {
    if (s->top - s->base == s->stackSize) {
        // 在栈的头部追加分配 STACKINCRENT 内存
        s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCRENT) * sizeof(ElemType));
        if (!s->base)//判断这个栈是否存在
            exit(0);
    }
    *(s->top) = e;//存入数据
    s->top++;//栈顶指针上移一位
}

//	出栈
ElemType Pop(Stack *s) {
    if (s->top == s->base)
        return 0;
    int e = *--(s->top);//将栈顶元素弹出并修改栈顶指针
    return e;
}

// 销毁栈
void DestroyStack(Stack *s) {
    int I, len;
    len = s->stackSize;
    for (I = 0; I < len; I++)
    {
        free(s->base);
        s->base++;
    }
    s->base = s->top = NULL;
    s->stackSize = 0;
}

int isEmpty(Stack s) {
    return  s.top - s.base == 0;
}